﻿using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;
using System.Globalization;
using Addin665.Commands;

namespace Addin665 {
    /// <summary>用于实现外接程序的对象。</summary>
    /// <seealso class='IDTExtensibility2' />
    public class Connect : IDTExtensibility2, IDTCommandTarget {
        /// <summary>
        /// 自定义的cmd管理器
        /// </summary>
        CommandManager _cmdMgr;
        /// <summary>实现外接程序对象的构造函数。请将您的初始化代码置于此方法内。</summary>
        public Connect() {
            if (_cmdMgr == null) {
                _cmdMgr = new CommandManager();

                _cmdMgr.Commands.Add(new AddHeaderInfoCmd());
                _cmdMgr.Commands.Add(new AddHeaderInfoCfgCmd());
                _cmdMgr.Commands.Add(new MultiGetSetCmd());
                _cmdMgr.Commands.Add(new MultiGetSetCfgCmd());
            }
        }

        /// <summary>实现 IDTExtensibility2 接口的 OnConnection 方法。接收正在加载外接程序的通知。</summary>
        /// <param term='application'>宿主应用程序的根对象。</param>
        /// <param term='connectMode'>描述外接程序的加载方式。</param>
        /// <param term='addInInst'>表示此外接程序的对象。</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) {
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;
            if (_cmdMgr != null) {
                _cmdMgr.CreateCommands(new object[] { _applicationObject, _addInInstance });
            }
            if (connectMode == ext_ConnectMode.ext_cm_UISetup) {
                object[] contextGUIDS = new object[] { };
                Commands2 commands = (Commands2)_applicationObject.Commands;
                string toolsMenuName = "Tools";
                CommandBar stdCmdBar;

                //将此命令置于“工具”菜单上。
                //查找 MenuBar 命令栏，该命令栏是容纳所有主菜单项的顶级命令栏:
                Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

                //在 MenuBar 命令栏上查找“工具”命令栏:
                CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
                CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

                //如果希望添加多个由您的外接程序处理的命令，可以重复此 try/catch 块，
                //  只需确保更新 QueryStatus/Exec 方法，使其包含新的命令名。
                try {
                    ////将命令添加到 Commands 集合:
                    ////添加头部信息
                    Command AddHeaderInfoCmd = commands.AddNamedCommand2(_addInInstance, "AddHeaderInfoCmd", "X665", "添加自定义头部信息", true, 362, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton);
                    Command AddHeaderInfoCfgCmd = commands.AddNamedCommand2(_addInInstance, "AddHeaderInfoCfgCmd", "X665", "设置添加自定义头部信息", true, 52, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton);

                    ////批量 GET SET
                    Command MultiGetSetCmd = commands.AddNamedCommand2(_addInInstance, "MultiGetSetCmd", "X665", "批量get set", true, 451, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton);
                    Command MultiGetSetCfgCmd = commands.AddNamedCommand2(_addInInstance, "MultiGetSetCfgCmd", "X665", "设置批量get set", true, 52, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton);


                    ////对应于该命令的控件添加到“665”工具栏:
                    CommandBars cmsBars = _applicationObject.CommandBars as CommandBars;
                    stdCmdBar = cmsBars.Add("X665");

                    (AddHeaderInfoCmd.AddControl(stdCmdBar, 1) as CommandBarControl).Caption = "AddHeaderInfo";
                    (MultiGetSetCmd.AddControl(stdCmdBar, 2) as CommandBarControl).Caption = "MultiGetSet";

                    (AddHeaderInfoCfgCmd.AddControl(stdCmdBar, 3) as CommandBarControl).Caption = "AddHeaderInfoCmd";
                    (MultiGetSetCfgCmd.AddControl(stdCmdBar, 4) as CommandBarControl).Caption = "MultiGetSetCfg";

                    //for (int i = 400; i < 500; i++) {
                    //    Command cmd = commands.AddNamedCommand2(_addInInstance, i.ToString(), i.ToString(), "xx", true, i, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                    //    cmd.AddControl(stdCmdBar, 1);
                    //}
                }
                catch (System.ArgumentException ex) {
                    CommonHelp.LogHelper.Error(ex);
                }
            }
        }

        /// <summary>实现 IDTExtensibility2 接口的 OnDisconnection 方法。接收正在卸载外接程序的通知。</summary>
        /// <param term='disconnectMode'>描述外接程序的卸载方式。</param>
        /// <param term='custom'>特定于宿主应用程序的参数数组。</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) {
        }

        /// <summary>实现 IDTExtensibility2 接口的 OnAddInsUpdate 方法。当外接程序集合已发生更改时接收通知。</summary>
        /// <param term='custom'>特定于宿主应用程序的参数数组。</param>
        /// <seealso class='IDTExtensibility2' />		
        public void OnAddInsUpdate(ref Array custom) {
        }

        /// <summary>实现 IDTExtensibility2 接口的 OnStartupComplete 方法。接收宿主应用程序已完成加载的通知。</summary>
        /// <param term='custom'>特定于宿主应用程序的参数数组。</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnStartupComplete(ref Array custom) {
        }

        /// <summary>实现 IDTExtensibility2 接口的 OnBeginShutdown 方法。接收正在卸载宿主应用程序的通知。</summary>
        /// <param term='custom'>特定于宿主应用程序的参数数组。</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnBeginShutdown(ref Array custom) {
        }

        /// <summary>实现 IDTCommandTarget 接口的 QueryStatus 方法。此方法在更新该命令的可用性时调用</summary>
        /// <param term='commandName'>要确定其状态的命令的名称。</param>
        /// <param term='neededText'>该命令所需的文本。</param>
        /// <param term='status'>该命令在用户界面中的状态。</param>
        /// <param term='commandText'>neededText 参数所要求的文本。</param>
        /// <seealso class='Exec' />
        public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText) {
            try {
                if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone) {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                }
            }
            catch (System.ArgumentException ex) {
                CommonHelp.LogHelper.Error(ex);
            }
        }

        /// <summary>实现 IDTCommandTarget 接口的 Exec 方法。此方法在调用该命令时调用。</summary>
        /// <param term='commandName'>要执行的命令的名称。</param>
        /// <param term='executeOption'>描述该命令应如何运行。</param>
        /// <param term='varIn'>从调用方传递到命令处理程序的参数。</param>
        /// <param term='varOut'>从命令处理程序传递到调用方的参数。</param>
        /// <param term='handled'>通知调用方此命令是否已被处理。</param>
        /// <seealso class='Exec' />
        public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) {
            //handled = false;
            //if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            //{
            //    if(commandName == "Addin665.Connect.Addin665")
            //    {
            //        handled = true;
            //        return;
            //    }
            //}
            _cmdMgr.ActiveCommand(commandName.Split('.')[2]);
        }
        private DTE2 _applicationObject;
        private AddIn _addInInstance;
    }
}